﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class tictactoe : MonoBehaviour {
    private int turn = 1;//player1 is 1,player2 is 2
    private int[,] chessBoard = new int[3,3];//0 for blank,1 for p1,2 for p2
    private float box_len = 100;
    private float scrmid = Screen.width / 2;
    public GUIStyle pushed_box;

    void Start() {
        reset();
    }

    void OnGUI() {      
        GUI.Box(new Rect(scrmid - 160, 110, 320, 500), "Tic-Tac-Toe");// Main container
        if (GUI.Button(new Rect(scrmid - 50, 520, 100, 40), new GUIContent("Reset", "Click to restart"))) 
            reset();

        int situation = check();
        display(situation);
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (chessBoard[i,j] == 1) {
                    GUI.Button(new Rect(scrmid - 1.5f * box_len + i * box_len, 200 + j * box_len, box_len, box_len), "O", pushed_box);
                }
                else if (chessBoard[i,j] == 2) {
                    GUI.Button(new Rect(scrmid - 1.5f * box_len + i * box_len, 200 + j * box_len, box_len, box_len), "X", pushed_box);
                }   
                if(GUI.Button(new Rect(scrmid - 1.5f * box_len + i * box_len, 200 + j * box_len, box_len, box_len), "")) { 
                    if (situation == 0) {
                        if (turn == 1) {
                            chessBoard[i,j] = turn;
                            turn = 2;
                        }
                        else if(turn == 2){
                            chessBoard[i,j] = turn;
                            turn = 1;
                        }
                    }  
                }
            }
        }
        
    }
 
    /*check the situation*/
    int check() {   
        //check x-axis  
        for (int i = 0; i < 3; i++) {
            if (chessBoard[i,0] != 0 && chessBoard[i,0] == chessBoard[i,1] && chessBoard[i,1] == chessBoard[i,2]) {
                return chessBoard[i,0];
            }
        }
        //check y-axis
        for (int i = 0; i < 3; i++) {
            if (chessBoard[0,i] != 0 && chessBoard[0,i] == chessBoard[1,i] && chessBoard[1,i] == chessBoard[2,i]) {
                return chessBoard[0,i];
            }
        }
        //check slash
        if (chessBoard[1,1] != 0 && chessBoard[0,0]== chessBoard[1,1] && chessBoard[2,2] == chessBoard[1,1] ||
            chessBoard[0,2] == chessBoard[1,1] && chessBoard[2,0] == chessBoard[1,1]) {
            return chessBoard[1,1];
        }
        //check end
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; ++j) {
                if (chessBoard[i,j] == 0) return 0;
            }
        }
        //end but no one win,return a tie
        return 3;
    }
    /*Reset the game*/
    void reset() {
        turn = 1;//player1 first
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                chessBoard[i,j] = 0;//each board is blank
            }
        }
    }
    /*display the situation*/
    void display(int situation) {
        if (situation == 0) {
            if (turn == 1)
                GUI.Box(new Rect(scrmid - 50, 160, 100, 35), "Player1's turn");
            else if(turn == 2)
                GUI.Box(new Rect(scrmid - 50, 160, 100, 35), "Player2's turn");
        }
        else if (situation == 1) {
            GUI.Box(new Rect(scrmid - 50, 160, 100, 35), "Player1 win!");
        }
        else if (situation == 2) {
            GUI.Box(new Rect(scrmid - 50, 160, 100, 35), "Player2 win!");
        }
        else if (situation == 3) {
            GUI.Box(new Rect(scrmid - 50, 160, 100, 35), "A tie!");
        }
    }

    
}